package com.example.diarymanger.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.diarymanger.bean.ApiResponse;
import com.example.diarymanger.bean.DiaryBean;
import com.example.diarymanger.mapper.DiaryMapper;
import com.example.diarymanger.mapper.UserMapper;
import com.example.diarymanger.service.DiaryService;
import com.example.diarymanger.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service

public class DiaryServiceImpl extends ServiceImpl<DiaryMapper, DiaryBean> implements DiaryService {

    @Autowired
    private DiaryMapper diaryMapper;
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    /**
     * 保存日记
     *
     * @param diaryBean
     * @return
     */
    @Override
    public ResponseEntity<ApiResponse> saveDiary(DiaryBean diaryBean) {
        HashMap<Object, Object> response = new HashMap<>();
        logger.info("Saving diary with title: {}", diaryBean.getDiaryTitle());
        System.out.println("diaryBean=" + diaryBean);
        try {
            Integer maxDiaryId = diaryMapper.selectMaxDiaryId();
            maxDiaryId = (maxDiaryId == null) ? 10000 : maxDiaryId;
            int nextUid = maxDiaryId + 1;
            diaryBean.setName(diaryBean.getName());
            diaryBean.setUid(diaryBean.getUid());
            diaryBean.setDiaryId(nextUid);
            System.out.println("isPubic=[" + diaryBean.getIsPublic() + "]");
            int insertResult = diaryMapper.insert(diaryBean);
            if (insertResult > 0) {
                logger.info("日记保存操作成功");
                return new ResponseEntity<>(new ApiResponse(true, "日记保存操作成功"), HttpStatus.CREATED);
            } else {
                logger.error("日记保存操作未成功");
                return new ResponseEntity<>(new ApiResponse(false, "日记保存操作未成功"), HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            logger.error("日记保存操作发生异常: ", e);
            return new ResponseEntity<>(new ApiResponse(false, "日记保存操作发生异常"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据用户id查询所有日记
     * @param userId
     * @return
     */
    @Override
    public ResponseEntity<List<DiaryBean>> getDiariesByUserId(Long userId) {
        QueryWrapper<DiaryBean> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", userId);
        List<DiaryBean> diaryList = diaryMapper.selectList(wrapper);
        return ResponseEntity.ok(diaryList);
    }

    /**
     * 根据日记id查询日记
     * @param diaryId
     * @return
     */
    @Override
    public DiaryBean getDiaryByDiaryId(Integer diaryId) {
        QueryWrapper<DiaryBean> wrapper = new QueryWrapper<>();
        wrapper.eq("diary_id", diaryId);
        DiaryBean diaryBean = diaryMapper.selectOne(wrapper);
        if (diaryBean == null) {
            System.out.println("该日记号对应的日记没找到，DiaryId=[" + diaryId + "]");
            return null;
        }
        return diaryBean;
    }

    /**
     * 根据diaryId更新日记
     * @param diaryBean
     * @return
     */
    @Override
    public boolean updateDiary(DiaryBean diaryBean) {
        QueryWrapper<DiaryBean> wrapper = new QueryWrapper<>();
        wrapper.eq("diary_id", diaryBean.getDiaryId());
        // 更新日记信息
        return this.update(diaryBean, wrapper);
    }
    /**
     * 根据日记id删除日记
     *
     * @param diaryId
     * @return
     */
    @Override
    public boolean deleteByDiaryId(Integer diaryId) {
        QueryWrapper<DiaryBean> wrapper = new QueryWrapper<>();
        wrapper.eq("diary_id", diaryId);
        return remove(wrapper);
    }
    /**
     * 根据是否公开查询所有日记
     * @return
     */
    @Override
    public ResponseEntity<List<DiaryBean>> getIsPublicDiaryAll() {
        QueryWrapper<DiaryBean> wrapper = new QueryWrapper<>();
        wrapper.eq("is_public", 1);
        List<DiaryBean> diaryList = diaryMapper.selectList(wrapper);
        return ResponseEntity.ok(diaryList);
    }


}
